﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace QuangMobile
{
    public partial class Search : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["price"] != null)
            {
                double price = Convert.ToDouble(Request.QueryString["price"]);
                LoadSanPham(price);
            }
            else
            {
                if (Request.QueryString["q"] != null && Request.QueryString["hang"] != null)
                {
                    string q = Request.QueryString["q"].ToString();
                    int hang = Convert.ToInt32(Request.QueryString["hang"].ToString());
                    LoadSanPhamTimKiem(q, hang);
                }
            }
        }

        private void LoadSanPhamTimKiem(string q, int hang)
        {

            string sql = "SELECT * FROM dbo.tblWebSanPham WHERE TenSP like N'%" + q + "%' AND HangID_DM = @Hang";
            List<SqlParameter> arrParams = new List<SqlParameter>();
            arrParams.Add(SqlHelper.CreateParameterObject("@Hang", SqlDbType.Int, ParameterDirection.Input, hang));
            ProductCategory.Text = "Kết quả tìm kiếm";
            DataTable dt = SqlHelper.ExecuteDataTable(CommandType.Text, sql, arrParams);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                UCProductItem Items = (UCProductItem)LoadControl("UCProductItem.ascx");
                Items.ProductID = Convert.ToInt32(dt.Rows[i]["ID"]);
                Items.ProductImage = dt.Rows[i]["AnhSP"].ToString();
                Items.ProductTitle = dt.Rows[i]["TenSP"].ToString();
                Items.ProductPrice = Convert.ToDouble(dt.Rows[i]["GiaSP"]);
                Items.ProductComment = dt.Rows[i]["MoTa"].ToString();
                ProductListCategory.Controls.Add(Items);
            }
        }

        private void LoadSanPham(double price)
        {
            double h = price * 1000000;
            double l = h - 1000000;
            if (price == 11)
            {
                h = 100000000;
            }
            string sql = "SELECT * FROM dbo.tblWebSanPham WHERE GiaSP >= @l AND GiaSP <= @h";
            List<SqlParameter> arrParams = new List<SqlParameter>();
            arrParams.Add(SqlHelper.CreateParameterObject("@l", SqlDbType.Float, ParameterDirection.Input, l));
            arrParams.Add(SqlHelper.CreateParameterObject("@h", SqlDbType.Float, ParameterDirection.Input, h));
            ProductCategory.Text = "Sản phẩm có giá từ " + l.ToString("0,00") + " vnđ đến " + h.ToString("0,00") + " vnđ.";
            DataTable dt = SqlHelper.ExecuteDataTable(CommandType.Text, sql, arrParams);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                UCProductItem Items = (UCProductItem)LoadControl("UCProductItem.ascx");
                Items.ProductID = Convert.ToInt32(dt.Rows[i]["ID"]);
                Items.ProductImage = dt.Rows[i]["AnhSP"].ToString();
                Items.ProductTitle = dt.Rows[i]["TenSP"].ToString();
                Items.ProductPrice = Convert.ToDouble(dt.Rows[i]["GiaSP"]);
                Items.ProductComment = dt.Rows[i]["MoTa"].ToString();
                ProductListCategory.Controls.Add(Items);
            }

        }
    }
}
